<?php

namespace App\Http\Controllers\Api;

//use Illuminate\Http\Request;
use App\Http\Requests\Api\RefereeQrcodeRequest;
use App\Models\RefereeQrcode;
use App\Http\Resources\RefereeQrcodeResource;
use App\Http\Controllers\Controller;
use App\Models\VehicleSeries;
use App\Models\RefereeBlack;

class RefereeQrcodeController extends Controller
{
    public function show(RefereeQrcodeRequest $request, RefereeQrcode $refereeQrcode)
    {
        $user = $request->user();

        if (is_null($user->rmore)) {
            return $this->failed(null, '请先前往注册成为推荐官');
        }
        if (RefereeBlack::phoneExists($user->rmore->phone)) {
            return $this->failed('in-blacklist');
        }

        $series_id = $request->series_id;
        $row = $refereeQrcode->where('user_id', $user->id)->where('vehicle_series_id', $series_id)->first();
        if (is_null($row)) {
            $vehicle_series = VehicleSeries::find($series_id);
            $scene = 'referee_id=' . $user->id . '&series_id=' . $series_id;
            $url = 'pages/activity/carList/index';
            $optional = ['width' => 300, 'auto_color' => false, 'env_version' => 'release', 'check_path' => false]; //trial
            $qrcode = app('App\Services\WechatService')->getUnlimitedQRCode($url, $scene, $optional);
            $refereeQrcode->user_id = $user->id;
            $refereeQrcode->vehicle_series_id = $series_id;
            $refereeQrcode->qrcode = $qrcode;
            $refereeQrcode->share_poster = app('App\Services\RefereeService')->getRecommendPoster($user->id, $series_id, $qrcode, $vehicle_series->recommend_poster, $user->rmore->name);
            $refereeQrcode->save();
            $row = $refereeQrcode;
        }
        return $this->resource(new RefereeQrcodeResource($row));
    }
}
